import pygtk
pygtk.require('2.0')
import gtk
import assessment
import os
import lesson

TYPING_ROOT = os.path.dirname(os.path.abspath(__file__))
while(os.path.isfile(TYPING_ROOT)):
    TYPING_ROOT = os.path.dirname(TYPING_ROOT)
IMAGE_SOURCE = TYPING_ROOT + os.sep + "images" + os.sep + "assessment" 

class assessmentChooser(gtk.VBox):

    def __init__(self, window):
        super(assessmentChooser, self).__init__(False, 0)

        self.win = window
        self.deleter = self.win.connect("delete_event", self.delete_event)

        self.choices = gtk.HBox(False, 0)

        self.add(self.choices)

        self.choices.show()
        
        self.buttonList = []
        self.useSound = True
        self.useImage = True
        self.useWord = True

        #Create Buttons: Sound, Images, Words
        self.createButton("sound")
        self.createButton("image")
        self.createButton("word")


        image = gtk.Image()
        image.set_from_file(IMAGE_SOURCE + os.sep + "assessment.gif")
        image.show()
        self.goButton = gtk.Button()
        self.goButton.set_image(image)
        self.goButton.show()
        self.add(self.goButton)
        self.goButton.connect("clicked", self.start)
        self.buttonList.append(self.goButton)

        button = gtk.Button()
        button.show()
        self.add(button)
        self.buttonList.append(button)
        backimage = gtk.Image()
        backimage.set_from_file(IMAGE_SOURCE + os.sep + "backbutton.gif")
        backimage.show()
        button.set_image(backimage)
        button.connect("clicked", self.back, "Back")
        
        self.quit = False
        
    def start(self, widget):
        if(self.useImage or self.useSound or self.useWord):
            for x in self.buttonList:
                x.hide()

            #Load up all the lessons
            lessons = lesson.Lessons()
            lessons.loadLessonPlan(TYPING_ROOT + os.sep + "lesson.ln")
                
            self.AM = assessment.AssessmentMode(self.win, lessons,self.useSound,self.useImage,self.useWord)
            self.add(self.AM)
            self.AM.show()

            #Block the close window trigger, so we only call it once        
            self.win.handler_block(self.deleter)
            
            quit = self.AM.runGame("")
            if(quit):
                self.delete_event(False, False, False)
                return
            
            #Unblock the trigger, so closing the window once again ends the program        
            self.win.handler_unblock(self.deleter)

            self.AM.hide()
            del self.AM

            for x in self.buttonList:
                x.show()
        

    #Hides all these buttons, and then quits back up to the mode chooser
    def back(self, widget, data=None):
        self.hide()
        gtk.main_quit()

    def delete_event(self, widget, event, data=None):
        self.quit = True
        gtk.main_quit()
        return False

    def createButton(self, word):
        image = gtk.Image()
        image.set_from_file(IMAGE_SOURCE + os.sep + word + ".gif")
        image.show()
        choose = gtk.CheckButton()
        choose.set_image(image)
        choose.show()
        self.choices.add(choose)
        choose.connect("toggled", self.toggle, word)
        choose.set_active(True)
        self.buttonList.append(choose)

    def run(self):
        gtk.main()
        if(not self.quit):
            self.win.disconnect(self.deleter)
        return self.quit
        
    def toggle(self, toggleButton, toggleHit=None):
        if(toggleHit == "sound"):
            self.useSound = toggleButton.get_active()
        elif(toggleHit == "image"):
            self.useImage = toggleButton.get_active()
        elif(toggleHit == "word"):
            self.useWord = toggleButton.get_active()